#include "smelter_music.h"
#include "helper.h"
#include "debug.h"
#define MAX_MUSIC_IDEL_TIME 1000      //30秒钟
#define MAX_MUSIC_IDELEFFECT_TIME 100 //3秒
#define TRAINSION_SPEED 1
typedef struct
{
    su8 hisPeakR;
    su8 hisPeakL;
    su16 timeOutCnt;
    Helper_Ease_Context easePeakR;
    Helper_Ease_Context easePeakL;

    Helper_Ease_Context easeRR;
    Helper_Ease_Context easeRG;
    Helper_Ease_Context easeRB;

    Helper_Ease_Context easeLR;
    Helper_Ease_Context easeLG;
    Helper_Ease_Context easeLB;
} Smelter_Music_Context;

static Smelter_Music_Context context;

void smelter_easeParse(Config_Mode_Music *music)
{
    su16 tmp = 0;
    su16 tmp2 = 0;

    if (gCfgModeFactoryPtr->mode_music_isSwitchRL)
    {
        context.easePeakL.target = music->peakR;
        context.easePeakR.target = music->peakL;

        context.easeLR.target = music->colorR.R;
        context.easeLG.target = music->colorR.G;
        context.easeLB.target = music->colorR.B;
        context.easeRR.target = music->colorL.R;
        context.easeRG.target = music->colorL.G;
        context.easeRB.target = music->colorL.B;
    }
    else
    {
        context.easePeakR.target = music->peakR;
        context.easePeakL.target = music->peakL;

        context.easeRR.target = music->colorR.R;
        context.easeRG.target = music->colorR.G;
        context.easeRB.target = music->colorR.B;
        context.easeLR.target = music->colorL.R;
        context.easeLG.target = music->colorL.G;
        context.easeLB.target = music->colorL.B;
    }
    helper_easeGenerate(&context.easeLR);
    helper_easeGenerate(&context.easeLG);
    helper_easeGenerate(&context.easeLB);
    helper_easeGenerate(&context.easeRR);
    helper_easeGenerate(&context.easeRG);
    helper_easeGenerate(&context.easeRB);
    helper_easeGenerate(&context.easePeakL);
    helper_easeGenerate(&context.easePeakR);
}

void smelter_music_originBothSide(void)
{
    su16 tmp = 0;
    su16 tmp2 = 0;
    su8 rlDot = 0;
    //将数据缩放至长度
    //刷新到起始点

    rlDot = helper_cacl_precentage_denoinator255((gCfgFactoryPtr->enabledDotNumber >> 1) - gCfgModeFactoryPtr->mode_music_start_offset, context.easePeakL.key);

    for (tmp = 0; tmp < rlDot; tmp++)
    {
        tmp2 = tmp + gCfgModeFactoryPtr->mode_music_start_offset;
        rgbStrip.colors[tmp2].R = context.easeLR.key;
        rgbStrip.colors[tmp2].G = context.easeLG.key;
        rgbStrip.colors[tmp2].B = context.easeLB.key;
    }

    rlDot = helper_cacl_precentage_denoinator255((gCfgFactoryPtr->enabledDotNumber >> 1) - gCfgModeFactoryPtr->mode_music_end_offset, context.easePeakR.key);
    for (tmp = 0; tmp < rlDot; tmp++)
    {
        tmp2 = gCfgFactoryPtr->enabledDotNumber - tmp - gCfgModeFactoryPtr->mode_music_end_offset;
        rgbStrip.colors[tmp2].R = context.easeRR.key;
        rgbStrip.colors[tmp2].G = context.easeRG.key;
        rgbStrip.colors[tmp2].B = context.easeRB.key;
    }
}
void smelter_music_symmetryBothSide(void)
{
    su16 tmp = 0;
    su16 tmp2 = 0;
    su8 rlDot = 0;
    //将数据缩放至长度
    //刷新到起始点

    rlDot = helper_cacl_precentage_denoinator255((gCfgFactoryPtr->enabledDotNumber >> 1) - gCfgModeFactoryPtr->mode_music_start_offset, context.easePeakL.key);
    for (tmp = 0; tmp < rlDot; tmp++)
    {

        if (tmp % 2)
        {
            tmp2 = (tmp >> 1) + gCfgModeFactoryPtr->mode_music_start_offset;
            rgbStrip.colors[tmp2].R = context.easeLR.key;
            rgbStrip.colors[tmp2].G = context.easeLG.key;
            rgbStrip.colors[tmp2].B = context.easeLB.key;
        }
        else
        {

            tmp2 = (gCfgFactoryPtr->enabledDotNumber >> 1) - (tmp >> 1);
            rgbStrip.colors[tmp2].R = context.easeLR.key;
            rgbStrip.colors[tmp2].G = context.easeLG.key;
            rgbStrip.colors[tmp2].B = context.easeLB.key;
        }
    }
    rlDot = helper_cacl_precentage_denoinator255((gCfgFactoryPtr->enabledDotNumber >> 1) - gCfgModeFactoryPtr->mode_music_end_offset, context.easePeakR.key);
    for (tmp = 0; tmp < rlDot; tmp++)
    {
        if (tmp % 2)
        {
            tmp2 = gCfgFactoryPtr->enabledDotNumber - (tmp >> 1) - gCfgModeFactoryPtr->mode_music_end_offset;
            rgbStrip.colors[tmp2].R = context.easeRR.key;
            rgbStrip.colors[tmp2].G = context.easeRG.key;
            rgbStrip.colors[tmp2].B = context.easeRB.key;
        }
        else
        {
            tmp2 = (gCfgFactoryPtr->enabledDotNumber >> 1) + (tmp >> 1);
            rgbStrip.colors[tmp2].R = context.easeRR.key;
            rgbStrip.colors[tmp2].G = context.easeRG.key;
            rgbStrip.colors[tmp2].B = context.easeRB.key;
        }
    }
}

void smelter_music_symetryMiddleBothSide(void)
{
    su16 tmp = 0;
    su16 tmp2 = 0;
    su8 rlDot = 0;
    //将数据缩放至长度
    //刷新到起始点

    rlDot = helper_cacl_precentage_denoinator255((gCfgFactoryPtr->enabledDotNumber >> 1) - gCfgModeFactoryPtr->mode_music_start_offset, context.easePeakL.key);
    for (tmp = 0; tmp < rlDot; tmp++)
    {
        if (tmp % 2)
        {
            tmp2 = (gCfgFactoryPtr->enabledDotNumber >> 2) + (tmp >> 1) + (gCfgModeFactoryPtr->mode_music_start_offset >> 1);

            rgbStrip.colors[tmp2].R = context.easeLR.key;
            rgbStrip.colors[tmp2].G = context.easeLG.key;
            rgbStrip.colors[tmp2].B = context.easeLB.key;
        }
        else
        {
            tmp2 = (gCfgFactoryPtr->enabledDotNumber >> 2) - (tmp >> 1) + (gCfgModeFactoryPtr->mode_music_start_offset >> 1);
            rgbStrip.colors[tmp2].R = context.easeLR.key;
            rgbStrip.colors[tmp2].G = context.easeLG.key;
            rgbStrip.colors[tmp2].B = context.easeLB.key;
        }
    }

    rlDot = helper_cacl_precentage_denoinator255((gCfgFactoryPtr->enabledDotNumber >> 1) - gCfgModeFactoryPtr->mode_music_end_offset, context.easePeakR.key);
    for (tmp = 0; tmp < rlDot; tmp++)
    {
        if (tmp % 2)
        {
            tmp2 = (gCfgFactoryPtr->enabledDotNumber - (gCfgFactoryPtr->enabledDotNumber >> 2)) + (tmp >> 1) - (gCfgModeFactoryPtr->mode_music_end_offset >> 1);
            rgbStrip.colors[tmp2].R = context.easeRR.key;
            rgbStrip.colors[tmp2].G = context.easeRG.key;
            rgbStrip.colors[tmp2].B = context.easeRB.key;
        }
        else
        {
            tmp2 = (gCfgFactoryPtr->enabledDotNumber - (gCfgFactoryPtr->enabledDotNumber >> 2)) - (tmp >> 1) - (gCfgModeFactoryPtr->mode_music_end_offset >> 1);
            rgbStrip.colors[tmp2].R = context.easeRR.key;
            rgbStrip.colors[tmp2].G = context.easeRG.key;
            rgbStrip.colors[tmp2].B = context.easeRB.key;
        }
    }
}

void smelter_music_restart(Config_Mode_Music *music)
{
    context.easePeakL.slop = TRAINSION_SPEED;
    context.easePeakL.key = 0;
    context.easePeakR.slop = TRAINSION_SPEED;
    context.easePeakR.key = 0;
    context.easeLR.slop = TRAINSION_SPEED;
    context.easeLR.key = 0;
    context.easeLG.slop = TRAINSION_SPEED;
    context.easeLG.key = 0;
    context.easeLB.slop = TRAINSION_SPEED;
    context.easeLB.key = 0;

    context.easeRR.slop = TRAINSION_SPEED;
    context.easeRR.key = 0;
    context.easeRG.slop = TRAINSION_SPEED;
    context.easeRG.key = 0;
    context.easeRB.slop = TRAINSION_SPEED;
    context.easeRB.key = 0;

    context.timeOutCnt = 0;
    context.hisPeakL = 0;
    context.hisPeakR = 0;
}

Efct_Stage smelter_music_process(Config_Mode_Music *music)
{
    SF_Boolen isMusicIdel = SF_False;
    SF_Boolen isShowIdelEffect = SF_False;
    Config_Mode_Music tempMusic;
    su16 tmp;

    if (music->peakL != context.hisPeakL || music->peakR != context.hisPeakR)
    {
        context.hisPeakL = music->peakL;
        context.hisPeakR = music->peakR;
        context.timeOutCnt = 0;
        isMusicIdel = SF_False;
        isShowIdelEffect = SF_False;
    }
    else
    {
        if (context.timeOutCnt > MAX_MUSIC_IDEL_TIME)
        {
            isMusicIdel = SF_True;
        }
        context.timeOutCnt++;
    }
    smelter_easeParse(music); //解析音频数据，并进行柔滑

    switch (music->dispMode)
    {
    case O2Side:
        smelter_music_originBothSide();
        break;
    case OT2Side:
        smelter_music_symmetryBothSide();
        break;
    case M2Side:
        smelter_music_symetryMiddleBothSide();
        break;
    default:
        smelter_music_originBothSide();
        break;
    }

    if (isMusicIdel == SF_True)
    {
        return E_FINISHED;
    }
    else
    {
        return E_PROCESS;
    }
}